package service

import (
	"dao"
	"pojo/form"
	"pojo/model"
	"pojo/vo"
	"constant"
)

type NewsService struct {
	dao *dao.NewsDao
}

// 构造管理员服务层
func NewNewsService(dao *dao.NewsDao) *NewsService {
	return &NewsService{
		dao: dao,
	}
}

// 保存管理员信息
func (this *NewsService) InsertNews(newsForm *form.NewsForm) error {

	news := &model.News{
		Title:   newsForm.Title,
		Content: newsForm.Content,
		Img:     newsForm.Img,
	}

	return this.dao.InsertNews(news)
}

// 查询单个 新闻
func (this *NewsService) FindNews(id uint64) (*model.News, error) {

	return this.dao.FindNews(id)
}

// 分页查询 新闻
func (this *NewsService) QueryNews(queryForm *form.NewsPageQueryForm) *vo.TableVo {

	result := &vo.TableVo{}
	result.Code = constant.SUCCESS.Code
	result.Msg = constant.SUCCESS.Msg

	size, _ := this.dao.Count(queryForm)
	result.Total = size

	if size > 0 {
		news, _ := this.dao.List(queryForm)
		result.Items = news
	}

	return result

}

// 更新新闻
func (this *NewsService) UpdateNews(news *form.NewsForm) (error) {

	return this.dao.UpdateNews(news)
}

// 删除新闻
func (this *NewsService) DeleteNews(id uint64) error {

	return this.dao.DeleteNews(id)
}
